package zhibi.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zhibi.cms.domain.Pic;
import zhibi.cms.extra.base.BaseServiceImpl;
import zhibi.cms.extra.pojo.PicPo;
import zhibi.cms.mapper.PicMapper;
import zhibi.cms.mapper.PicMapper;
import zhibi.cms.service.PicService;
import zhibi.frame.domain.Page;
import zhibi.frame.mybatis.example.Example;
import zhibi.frame.mybatis.example.ExampleType;

import java.util.List;

@Service
@Transactional
public class PicServiceImpl extends BaseServiceImpl<Pic> implements PicService {

    @Autowired
    public PicServiceImpl(PicMapper mapper) {
        super(mapper);
        this.picMapper = mapper;
    }

    private PicMapper picMapper;

    @Override
    public PageInfo<PicPo> selectPoByExample(Example example, Page page) {
        if (null != page) PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return new PageInfo<>(picMapper.selectPoByExample(example), Page.NAV_SIZE);
    }

    @Override
    public PicPo selectBySiteFlagAndFlag(String siteFlag, String picFlag) {
        PageHelper.startPage(1, 1);
        Example example = Example.getInstance()
                .addParam("s.flag", siteFlag)
                .addParam("p.flag", picFlag)
                .addOrder("p.sort", ExampleType.OrderType.DESC);
        List<PicPo> list = picMapper.selectPoByExample(example);
        if (null == list || list.size() == 0) return null;
        return list.get(0);
    }
}
